#pragma once
#include <iostream>
#include <time.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

namespace mylog{
    namespace Util{
        class Data{
        public:
            static time_t Now(){
                return time(NULL);
            }
        };

        class FileUtil{
        public:
            static bool Exists(const std::string& path){
                struct stat st;
                return stat(path.c_str(), &st) == 0;
            }
            static std::string Path(const std::string& path){
                if(path.empty()) return ".";
                //size_t pos = path.rfind("/\\"); 出错
                size_t pos = path.find_last_of("/\\");
                if(pos == std::string::npos) return ".";
                return path.substr(0, pos + 1);
            }
            static bool CreateDir(const std::string& path){
                if(path.empty()) return false;
                if(Exists(path)) return true;
                int pos = 0, index = 0;
                while(index < path.size()){
                    pos = path.find_first_of("/\\", index);
                    if(pos == std::string::npos){
                        mkdir(path.c_str(), 0755);
                        break;
                    }
                    if(pos == index){ index = pos + 1; continue; }
                    std::string name = path.substr(0, pos);
                    if(Exists(name) == false){
                        mkdir(name.c_str(), 0755);
                    }
                    index = pos + 1;
                }
                return true;
            }
        };
    }
}